//
//  YKGuidePageController.m
//  YingKeZhuXiao
//
//  Created by admin on 16/6/24.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "YKGuidePageController.h"
#import "AppDelegate.h"
#import "UserModel.h"
#import "MyNavigationController.h"
#import "CKPuslichTool.h"
#import "LoginViewController.h"
#import "PicPassController.h"
#import "Header.h"

@interface YKGuidePageController ()<UIScrollViewDelegate>
#define FRAME_SIZE self.view.frame.size
@property (nonatomic, strong)  IBOutlet  UIScrollView      *scView;
@property (nonatomic, strong) UIImageView       *imgViewPic;
@property (nonatomic, strong) UIPageControl     *pageCon;

@property (nonatomic, strong) UIButton *btn;
@property (nonatomic, strong) NSString *pageType;
@end

@implementation YKGuidePageController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.pageType = @"fyh";
    [self createScrollerView];
 
}

#pragma mark - 外部设置启动图 Arr 对应方法
- (void)fyhGuidePageWithPicArr:(NSMutableArray *)picArr{
    self.picArr = picArr;
}

#pragma mark - 创建 VC
//+ (instancetype)sharedGuideVC {
//    static YKGuidePageController *guidePageVC = nil;
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        guidePageVC = [[YKGuidePageController alloc] init];
//    });
//    return guidePageVC;
//}
#pragma mark - ScrollerViewSomethings
- (void)createScrollerView {
//    self.scView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//    self.scView.delegate = self;//Delegate
//    [self.view addSubview:self.scView];
    self.scView.contentSize = CGSizeMake(3 * FRAME_SIZE.width, FRAME_SIZE.height);
    self.scView.pagingEnabled = YES;
    self.scView.bounces = NO;
    self.scView.showsHorizontalScrollIndicator = NO;
    
    //创建 imageView
    for (int i = 1; i <= self.picArr.count; i++) {
        self.imgViewPic = [[UIImageView alloc] initWithFrame:CGRectMake(FRAME_SIZE.width * (i - 1), 0, FRAME_SIZE.width, FRAME_SIZE.height+1)];
        self.imgViewPic.image = [self.picArr objectAtIndex:i - 1];//pic
        self.imgViewPic.tag = 100 + i;//tag
        [self.scView addSubview:self.imgViewPic];
        
        //        NSLog(@"picArr.count --- %lu", (unsigned long)self.picArr.count);
        //BtnSomethings self.picArr.count + 1
        if (i == 3) {//pic 起始数为1,Arr 起始为0,所以 lastIndex 需要加1 !!!!!!!!
            //            NSLog(@"lastPicArr --- %ld", (unsigned long)self.picArr.count);
           
            /** 启动页面 Btn 名称设置*/
            self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
            self.btn.frame = CGRectMake(2 * FRAME_SIZE.width + (FRAME_SIZE.width-300)/2, FRAME_SIZE.height - 100, 300, 100);
           
//            [self.btn setTitle:@"立即体验" forState:UIControlStateNormal];
//            [self.btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            [self.btn setBackgroundColor:[UIColor clearColor]];
            [self.btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];//点击事件
            [self.scView addSubview:self.btn];
         
        }
    }
}
#pragma mark - PageControl
//- (void)createPageControl {
//    self.pageCon = [[UIPageControl alloc] initWithFrame:CGRectMake((FRAME_SIZE.width - 40) / 2, FRAME_SIZE.height - 50, 40, 20)];
//    self.pageCon.numberOfPages = 4;
//    self.pageCon.currentPageIndicatorTintColor = MAIN_COLOR_Blue;//IndexBgColor
//    self.pageCon.pageIndicatorTintColor = [UIColor whiteColor];//BgColor
//    [self.view addSubview:self.pageCon];
//}
#pragma mark - 相关方法
/** ScrollView 协议方法相关*/
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.pageCon.currentPage = self.scView.contentOffset.x / FRAME_SIZE.width;\
    //    NSLog(@"pageCon.currentPage --- %d", self.pageCon.currentPage);
}
/** Btn 点击事件*/
- (void)btnClick:(UIButton *)btn {
    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    DEF_PERSISTENT_SET_OBJECT(version, @"showGuideVersion");
    AppDelegate *tempAppDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

//    if ([UserModel isLogin]) {
//        PicPassController *picVc=(PicPassController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Login" viewID:@"PicPassController"];
//        MyNavigationController *nav = [[MyNavigationController alloc]initWithRootViewController:picVc];
//        tempAppDelegate.window.rootViewController=nav;
//        
//    }else{
//        LoginViewController *Login = (LoginViewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Login" viewID:@"LoginViewController"];
//        MyNavigationController *nav = [[MyNavigationController alloc]initWithRootViewController:Login];
//        tempAppDelegate.window.rootViewController=nav;
//    }

    NSString *oldPass=[[NSUserDefaults standardUserDefaults]objectForKey:@"PICPASSCODE"];
    if (oldPass.length>3) {
        PicPassController *picVc=(PicPassController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Login" viewID:@"PicPassController"];
        MyNavigationController *nav = [[MyNavigationController alloc]initWithRootViewController:picVc];
        tempAppDelegate.window.rootViewController=nav;
    }else{
        LoginViewController *Login = (LoginViewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Login" viewID:@"LoginViewController"];
        MyNavigationController *nav = [[MyNavigationController alloc]initWithRootViewController:Login];
        tempAppDelegate.window.rootViewController=nav;
    }
    

    
    
   
}
#pragma mark --- 旋转动画
- (void)rotate360DegreeWithImageView:(UIImageView *)imageView
{
    CABasicAnimation *animation = [ CABasicAnimation
                                   animationWithKeyPath: @"transform" ];
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    //围绕Z轴旋转，垂直与屏幕
    animation.toValue = [ NSValue valueWithCATransform3D:
                         CATransform3DMakeRotation(M_PI, 0.0, 0.0, 1.0) ];
    animation.duration = 5;
    //旋转效果累计，先转180度，接着再旋转180度，从而实现360旋转
    animation.cumulative = YES;
    animation.repeatCount = MAXFLOAT;
    //在图片边缘添加一个像素的透明区域，去图片锯齿
    CGRect imageRrect = CGRectMake(0, 0,imageView.frame.size.width, imageView.frame.size.height);
    UIGraphicsBeginImageContext(imageRrect.size);
    [imageView.image drawInRect:CGRectMake(1,1,imageView.frame.size.width-2,imageView.frame.size.height-2)];
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    [imageView.layer addAnimation:animation forKey:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
